package safari.controller.free;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import safari.model.common.FreeListVO;
import safari.model.common.ListVO;
import safari.model.common.SearchVO;
import safari.model.free.FreeBoardCommentVO;
import safari.model.free.FreeBoardService;
import safari.model.free.FreeBoardVO;
import safari.model.member.MemberVO;

//자유게시판 담당 컨트롤러
public class FreeBoardController extends MultiActionController {
	private FreeBoardService fs;
	//xml 객체 주입을 받는다.
	public FreeBoardController(FreeBoardService fs){
		this.fs = fs;
	}
	/**
	 * 자유 게시판 리스트 출력 기본 1페이지 page 요청시 해당 페이지 리스트 출력
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 */
	public String freeBoardGetAllList(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		ListVO fList = fs.freeBoardGetAllList(request.getParameter("page"));
		request.setAttribute("freeboard", fList);
		return "free/list";
	}
	/**
	 * 자유게시판 게시글 상세보기
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 */
	public String freeBoardShowContent(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws SQLException{
		System.out.println("freeBoardShowContent : " + request.getParameter("no"));
		System.out.println("freeBoardShowContent : " + request.getParameter("page"));
		MemberVO mvo = (MemberVO)session.getAttribute("userInfo");
		FreeListVO fListVO = fs.freeBoardShowContent(request.getParameter("no"), request.getParameter("page"));
		request.setAttribute("freeboard", fListVO);
		return "free/showContent";
	}
	/**
	 * 자유게시판 게시물 작성 페이지 이동
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 */
	public String freeBoardRegisterView(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		return "free/register";
	}
	/**
	 * 자유게시판 게시물 등록
	 * @param request
	 * @param response
	 * @param session
	 * @param paramFBVO
	 * @return
	 * @throws SQLException
	 */
	public String freeBoardRegister(HttpServletRequest request, HttpServletResponse response, HttpSession session, FreeBoardVO paramFBVO) throws SQLException{
		MemberVO mvo = (MemberVO)session.getAttribute("userInfo");
		paramFBVO.setId(mvo.getId());
		fs.freeBoardRegister(paramFBVO);
		return "redirect:/freeboard.do?command=freeBoardShowContent&no="+paramFBVO.getNo();
	}
	/**
	 * 자유게시판 게시글 업데이트 페이지 이동
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 */
	public String freeBoardupdateView(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		int no = Integer.parseInt(request.getParameter("no"));
		FreeBoardVO fbvo = fs.freeBoardUpdateView(no);
		request.setAttribute("fbvo", fbvo);	
		return "free/updateView";
	} 
	/**
	 * 자유게시판 페이지 업데이트
	 * @param request
	 * @param response
	 * @param session
	 * @param fbvo
	 * @return
	 * @throws SQLException
	 */
	public String freeBoardUpdate(HttpServletRequest request, HttpServletResponse response,HttpSession session, FreeBoardVO fbvo) throws SQLException{
		fs.freeBoardUpdate(fbvo);
		return "redirect:/freeboard.do?command=freeBoardShowContent&no="+fbvo.getNo();
	}
	/**
	 * 자유게시판 게시글 삭제
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 */
	public String freeBoardDelete(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		int no = Integer.parseInt(request.getParameter("no"));
		fs.freeBoardDelete(no);
		return "redirect:/freeboard.do?command=freeBoardGetAllList";
	}
	/**
	 * 자유게시판 답글 보여주는 페이지로 이동
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 */
	public String freeBoardReplyView(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		FreeBoardVO fbvo = fs.freeBoardReplyView(request.getParameter("no"));
		request.setAttribute("fbvo", fbvo);
		return "free/replyView";
	} 
	/**
	 * 자유게시판 답글 등록 
	 * @param request
	 * @param response
	 * @param session
	 * @param paramFBVO
	 * @return
	 * @throws SQLException
	 */
	public String freeBoardReply(HttpServletRequest request, HttpServletResponse response, HttpSession session, FreeBoardVO paramFBVO) throws SQLException{
		MemberVO mvo = (MemberVO)session.getAttribute("userInfo");
		paramFBVO.setId(mvo.getId());
		fs.freeBoardReplyRegister(paramFBVO);
		return "redirect:/freeboard.do?command=freeBoardGetAllList";
	} 
	/**
	 * 자유게시판 덧글 등록
	 * @param request
	 * @param response
	 * @param session
	 * @param paramfbcvo
	 * @return
	 * @throws SQLException
	 */
	public ModelAndView freeBoardCommentRegister(HttpServletRequest request, HttpServletResponse response,HttpSession session, FreeBoardCommentVO paramfbcvo) throws SQLException{
		MemberVO mvo = (MemberVO)session.getAttribute("userInfo");
		paramfbcvo.setId(mvo.getId());
		int comf = Integer.parseInt(request.getParameter("comf"));
		paramfbcvo.setComf(comf);
		paramfbcvo.setId(mvo.getId());
		List<FreeBoardCommentVO> fbcvo = fs.freeBoardCommentRegisterAndShowComment(paramfbcvo);
		return new ModelAndView("JsonView","commentList",fbcvo);
	}
	/**
	 * 자유게시판 comment 등록 및 comment list 전송
	 * @param request
	 * @param response
	 * @param session
	 * @param paramfbcvo
	 * @return
	 * @throws SQLException
	 */
	public String freeBoardCommentDelete(HttpServletRequest request, HttpServletResponse response,HttpSession session, FreeBoardCommentVO paramfbcvo) throws SQLException{
		int no = Integer.parseInt(request.getParameter("no"));
		String page = request.getParameter("page");
		int boardNO = Integer.parseInt(request.getParameter("boardNo"));
		fs.freeBoardCommentDelete(no);
		return"redirect:/freeboard.do?command=freeBoardShowContent&no="+ boardNO ;
	}
	/**
	 * 자유게시판 페이지에 해당하는 덧글 출력
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 */
	public ModelAndView freeBoardCommentGetAllList(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		String page = request.getParameter("page");
		return new ModelAndView("JsonView", "commentList",fs.freeBoardCommentGetAllList(page));
	}
	public String freeBoardSearchKeyword(HttpServletRequest request, HttpServletResponse response, SearchVO svo) throws SQLException{
		System.out.println("freeBoardSearchKeyword : " + svo.getKeyword());
		ListVO list = fs.freeBoardSearchKeyword(svo);
		System.out.println("list "+list);
		request.setAttribute("freeboard", list);
		return "free/list";
	}
	public String freeBoardCommentUpdateView(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		int no = Integer.parseInt(request.getParameter("no"));
		FreeBoardCommentVO fbcvo = fs.freeBoardCommentUpdateView(no);
		System.out.println("freeBoardCommentUpdateView : " + fbcvo);
		request.setAttribute("fbcvo", fbcvo);
		return "free/commentUpdateView";
	} 
	
	public String freeBoardCommentUpdate(HttpServletRequest request, HttpServletResponse response,FreeBoardCommentVO fbcvo) throws SQLException{
		fs.freeBoardCommentUpdate(fbcvo);
		return "redirect:/freeboard.do?command=freeBoardShowContent&no="+fbcvo.getComf();
	}
}
